#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Bootstrap Form Viewlet Module

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_45"
__date__    = "2014/05/04"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python
import sys
import platform
import socket
import traceback
import calendar
from random import Random
from datetime import datetime

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.core import MIKAN
from mikan.viewlet import MikanDivViewlet, MikanH2Viewlet, MikanHrViewlet, MikanFormViewlet, MikanJSViewlet, MikanButtonViewlet
from mikan.viewlet.formviewlet import AbstractMikanBaseFormViewlet


# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Mikan Contant
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

class MikanJSONFormViewlet(AbstractMikanBaseFormViewlet):

	def __init__(self, parent, action="", name="json_form", method="post", title="", url_json=None):
		super(MikanJSONFormViewlet, self).__init__(parent=parent, action=None, name=name, method=method, target=None)

		self.clazz = "row"
		self.FTitle = title
		self.FURLJSON = url_json

	def get_submit_caption(self):
		return u"実行"

	def get_back_caption(self):
		return u"戻る"

	def get_submit_code(self):
		return "mikan.form.confirm_json(\'form\', \'' . $formname . '\', \'' . $post_json . '\');"

	def get_back_code(self):
		return "mikan.form.html.back_json(\'form\', \'' . $formname . '\') ;"

	def build_content(self):
		# フォーム
		f = self.formaction
		if self.islogdebug:
			self.logger.debug(f)
		form = MikanFormViewlet(self, action=f, name=self.form_name)
		form.clazz = "form-horizontal"
		form.id = self.form_name
		form.role = "form"
		form.onsubmit = "return false;"

		# フォーム本体
		row = MikanDivViewlet(form)
		row.clazz = "row"

		md12 = MikanDivViewlet(row)
		md12.clazz = "col-md-12"

		title = MikanH2Viewlet(md12)
		title.nodevalue = self.title

		MikanHrViewlet(md12)

		message = MikanDivViewlet(row)
		message.clazz = "col-md-offset-1 col-md-11"
		message.id = "form_message"

		content = MikanDivViewlet(row)
		content.clazz = "col-md-offset-0 col-md-12"
		content.id = "form"
		content.nodevalue = u"読みこみ中・・・"

		md12 = MikanDivViewlet(row)
		md12.clazz = "col-md-12"
		MikanHrViewlet(md12)

		# ボタン
		submit = MikanDivViewlet(row)
		submit.clazz = "col-md-offset-6 col-md-2"
		submit = MikanButtonViewlet(submit)
		submit.id = self.form_name + "_submit"
		submit.clazz = "btn btn-default btn-lg btn-block"
		submit.type = "button"
		submit.nodevalue = u"実行"
		submit.onclick = u"mikan.form.confirm_json('form', '%s', '%s');" % (self.form_name, self.formaction)

		back = MikanDivViewlet(row)
		back.clazz = "col-md-2"
		back = MikanButtonViewlet(back)
		back.id = self.form_name + "_back"
		back.clazz = "hidden btn btn-lg btn-block"
		back.type = "button"
		back.nodevalue = u"戻る"
		back.onclick = u"mikan.form.html.back_json('form', '%s');" % self.form_name

		js =	MikanJSViewlet(self.view.body)
		js.nodevalue = u"""
mikan.form.html.load_json('form', '%s', '%s');
""" % (self.url_json, self.FFormName)

	def get_title(self):
		return self.FTitle

	def get_url_json(self):
		return self.FURLJSON

	# ----------------------------------------------------
	## Property
	title = ESiPCProperty(get_title, None, None)
	url_json = ESiPCProperty(get_url_json, None, None)

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------

# ------------------------------------------------------------
